//
//  ThemeManager.m
//  Project_2_weibo
//
//  Created by 林林 on 16/6/17.
//  Copyright © 2016年 lhn. All rights reserved.
//

#import "ThemeManager.h"
#define kThemeNameKey @"kThemeNameKey"

@interface ThemeManager ()
{
    NSDictionary *_textColorConfigDic;
}

@property (nonatomic, strong) NSDictionary *themeDic;

@end

@implementation ThemeManager

+(instancetype)sharedManager {
    
    static ThemeManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (manager == nil) {
            manager = [[super allocWithZone:nil] init];
            //初始化主题字典
            manager.themeDic = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"theme" ofType:@"plist"]];
            //加载config文件
            [manager loadConfigPlist];
        }
    });
    return manager;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [self sharedManager];
}

- (id)copy {
    return self;
}

- (NSString *)themeName {
    
    if (_themeName == nil) {
        
        //读取本地缓存的主题名字
        NSString *themeName = [[NSUserDefaults standardUserDefaults] objectForKey:kThemeNameKey];
        if (themeName == nil) {
            themeName = @"猫爷";
            [[NSUserDefaults standardUserDefaults] setObject:themeName forKey:kThemeNameKey];
        }
        _themeName = themeName;
    }
    
    return _themeName;

}

//加载config文件
- (void)loadConfigPlist {
    
    //根据当前主题名找到主题路径
    NSString *filePath = _themeDic[self.themeName];
    //拼接plist文件路径
    NSString *configPath = [NSString stringWithFormat:@"%@/%@", filePath, @"config"];
    //完整路径
    configPath = [[NSBundle mainBundle] pathForResource:configPath ofType:@"plist"];
    //加载字典
    _textColorConfigDic = [[NSDictionary alloc] initWithContentsOfFile:configPath];
}

//当主题发生改变时发送通知
- (void)setThemeName:(NSString *)name {
    
    if (![name isEqualToString:self.themeName]) {
        //只有在发生改变时才发送通知
        _themeName = [name copy];
        //重新加载主题文本字典
        [self loadConfigPlist];
        //将主题名保存到本地文件
        [[NSUserDefaults standardUserDefaults] setObject:_themeName forKey:kThemeNameKey];
        //发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:kThemeChangedNotificationName object:nil];
    }
}

//获取图片名
- (UIImage *)themeImageWithImageName:(NSString *)imageName {
    
    //根据当前主题名找到主题路径
    NSString *filePath = _themeDic[self.themeName];
    //拼接图片名
    NSString *imageFilePath = [NSString stringWithFormat:@"%@/%@", filePath, imageName];
    //返回图片
    return [UIImage imageNamed:imageFilePath];
}

//获取文本颜色
- (UIColor *)themeColorWithColorName:(NSString *)colorName {
    
    //从字典中获取主题对应的颜色数据
    NSDictionary *colorDic = _textColorConfigDic[colorName];
    if (colorDic == nil) {
        return nil;
    }
    //解析数据
    CGFloat red = [colorDic[@"R"] floatValue] / 255.0;
    CGFloat blue = [colorDic[@"B"] floatValue] / 255.0;
    CGFloat green = [colorDic[@"G"] floatValue] / 255.0;
    CGFloat alpha = [colorDic[@"alpha"] floatValue];
    //存在没有alpha情况
    if (alpha == 0) {
        alpha = 1;
    }
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
@end












